package com.sihai.ehr.web.action.train;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.TrainOrganizationCredit;
import com.sihai.ehr.model.biz.TrainPlanDutyTarget;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.service.TrainCreditService;
import com.sihai.ehr.service.TrainPlanDutyTargetService;
import com.sihai.ehr.web.action.EHRAction;

/**
 * 培训计划控制器
 * 
 * @author henry
 * 
 */
@Results({
		@Result(name = "main", location = "train_main.jsp"),
		@Result(name = "credit_set_manage", location = "credit_set_manage.jsp"),
		@Result(name = "list", location = "credit_set_list.jsp"),
		@Result(name = "credit_main", location = "credit_main.jsp"),
		@Result(name = "credit_list", location = "credit_list.jsp"),
		@Result(name = "credit_statis_list", location = "credit_statis_list.jsp") })
public class TrainOrganizationCreditAction extends EHRAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Log log = LogFactory.getLog(TrainOrganizationCreditAction.class);

	@BeanInject
	private OrganizationService organizationService;
	@BeanInject
	private TrainCreditService trainCreditService;
	@BeanInject
	private TrainPlanDutyTargetService trainPlanDutyTargetService; // 培训计划职务服务

	private List<Organization> organizations;
	private LimitedList<TrainOrganizationCredit> credits;
	private TrainOrganizationCredit credit;
	private Long creditId;
	private String ids;
	private LimitedList<TrainPlanDutyTarget> dutyTargets;

	private String module; // 模拟
	private int year;
	private List<String> searchyears;

	@Action("/train/credit_set_main")
	@Override
	public String execute() throws Exception {
		listTitle = "员工培训 - 学分设置";
		listAction = "/train/credit_set_list.xhtml";
		organizations = organizationService
				.findChildren(null, true, null, null);
		if (organizations != null && organizations.size() > 0) {
			if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
			organizationId = organizations.get(0).getId();
		}
		module = "train";
		Calendar mCalendar = Calendar.getInstance();
		if (searchyears == null) {
			searchyears = new ArrayList<String>();
		}
		searchyears.add(String.valueOf(mCalendar.get(Calendar.YEAR)));
		searchyears.add(String.valueOf(mCalendar.get(Calendar.YEAR) - 1));
		searchyears.add(String.valueOf(mCalendar.get(Calendar.YEAR) - 2));
		searchyears.add(String.valueOf(mCalendar.get(Calendar.YEAR) - 3));

		showChildOrg = true;
		return "main";
	}

	@Action("/train/credit_set_list")
	public String list() {
		listAction = "/train/credit_set_list.xhtml";
		credits = trainCreditService.getTrainOrganizationCredits(
				organizationId, -1L, year, createLimit(), createSorter());
		paging(credits);
		/* 设置翻页action */
		this.setPageUrl("train/credit_set_list.xhtml");
		return "list";
	}

	/**
	 * 转向培训室处理
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/train/credit_set_manage")
	public String dispatchCreditSet() throws Exception {
		if (creditId == null || creditId <= 0) {
			// 添加
			credit = new TrainOrganizationCredit();
			credit.setOrganization(organizationService.get(organizationId));
			listTitle = "添加"; 
		} else {
			// 修改
			credit = trainCreditService.getTrainOrganizationCredit(creditId);
			if (credit.getOrganization().getTopOrganization() == null) {
				organizationId = credit.getOrganization().getId();
			} else {
				organizationId = credit.getOrganization().getTopOrganization()
						.getId();
			}
			listTitle = "修改"; 
		}
		return "credit_set_manage";
	}

	/**
	 * 提交培训室处理
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/train/post_creditset")
	public String postCreditset() throws Exception {
		//
		if (credit == null) {
			if (log.isErrorEnabled()) {
				log.error("提交数据为空");
				return ERROR;
			}
		}
		if (credit.getId() == null) {
			// 添加
		} else {
			// 修改
		}
		try {
			trainCreditService.save(credit);
		} catch (Exception e) {
			log.equals(e.getMessage());
			return ERROR;
		}
		return "main";
	}

	/**
	 * 删除培训室
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/train/creditset_delete", results = { @Result(name = "success", type = "plainText") })
	public String deleteCreditset() throws Exception {
		if (StringUtils.isNotBlank(ids)) {
			try {
				trainCreditService.deleteTrainOrganizationCredit(ids);
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(e.getMessage());
				}
				return ERROR;
			}

		}

		return SUCCESS;
	}

	// ////学分管理////////////
	/**
	 * 选择培训目标
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/train/credit_main")
	public String dispatchCreditManage() throws Exception {
		listAction = "/train/credit_list.xhtml";
		listTitle = "学分管理";
		return "credit_main";
	}

	@Action("/train/credit_list")
	public String dispatchCreditList() throws Exception {
		listAction = "/train/credit_list.xhtml";
		// credits = trainCreditService.getTrainOrganizationCredits(
		// organizationId, -1L, createLimit(), createSorter());
		dutyTargets = trainPlanDutyTargetService
				.getTrainPlanDutyTargetsByOrganizationId(organizationId,
						createLimit(), createSorter());
		/* 分页 */
		paging(dutyTargets);
		/* 设置翻页action */
		this.setPageUrl("train/credit_list.xhtml");

		return "credit_list";
	}

	// /////////学分统计//////////
	@Action("/train/credit_statis_main")
	public String dispatchCreditStaticMain() throws Exception {
		listAction = "/train/credit_statis_list.xhtml";
		listTitle = "学分统计";
		organizations = organizationService
				.findChildren(null, true, null, null);
		if (organizations != null && organizations.size() > 0) {
			if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
			organizationId = organizations.get(0).getId();
		}
		module = "train";
		Calendar mCalendar = Calendar.getInstance();
		if (searchyears == null) {
			searchyears = new ArrayList<String>();
		}
		searchyears.add(String.valueOf(mCalendar.get(Calendar.YEAR)));
		searchyears.add(String.valueOf(mCalendar.get(Calendar.YEAR) - 1));
		searchyears.add(String.valueOf(mCalendar.get(Calendar.YEAR) - 2));
		searchyears.add(String.valueOf(mCalendar.get(Calendar.YEAR) - 3));
		showChildOrg = true;
		return "main";
	}

	@Action("/train/credit_statis_list")
	public String dispatchStatisCreditList() throws Exception {
		listAction = "/train/credit_statis_list.xhtml";
		credits = trainCreditService.getTrainOrganizationCredits(
				organizationId, -1L, year, createLimit(), createSorter());
		/* 分页 */
		paging(credits);
		/* 设置翻页action */
		this.setPageUrl("train/credit_statis_list.xhtml");

		return "credit_statis_list";
	}

	public void setOrganizations(List<Organization> organizations) {
		this.organizations = organizations;
	}

	public List<Organization> getOrganizations() {
		return organizations;
	}

	public LimitedList<TrainOrganizationCredit> getCredits() {
		return credits;
	}

	public void setCredits(LimitedList<TrainOrganizationCredit> credits) {
		this.credits = credits;
	}

	public TrainOrganizationCredit getCredit() {
		return credit;
	}

	public void setCredit(TrainOrganizationCredit credit) {
		this.credit = credit;
	}

	public Long getCreditId() {
		return creditId;
	}

	public void setCreditId(Long creditId) {
		this.creditId = creditId;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public LimitedList<TrainPlanDutyTarget> getDutyTargets() {
		return dutyTargets;
	}

	public void setDutyTargets(LimitedList<TrainPlanDutyTarget> dutyTargets) {
		this.dutyTargets = dutyTargets;
	}

	public String getModule() {
		return module;
	}

	public void setModule(String module) {
		this.module = module;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public List<String> getSearchyears() {
		return searchyears;
	}

	public void setSearchyears(List<String> searchyears) {
		this.searchyears = searchyears;
	}
}
